package com.project.market.controller;

import com.project.market.db.domain.MarketCoupon;
import com.project.market.service.AdminCouponService;
import com.project.market.service.impl.AdminCouponServiceImpl;
import com.project.market.util.JacksonUtil;
import com.project.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/coupon/*")
public class AdminCouponController extends HttpServlet {

    AdminCouponService adminCouponService = new AdminCouponServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getRequestURI().replace(req.getContextPath() + "/admin/coupon/", "");
        if("list".equals(op)) {
            list(req,resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");

        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String status = req.getParameter("status");

        List<MarketCoupon> list = adminCouponService.list(page,limit,sort,order,name,type,status);
        if(list == null){
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail(402,"参数值错误")));
        }else {
            Object ok = ResponseUtil.okList(list);
            String responseBody = JacksonUtil.writeValueAsString(ok);
            resp.getWriter().println(responseBody);
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getRequestURI().replace(req.getContextPath() + "/admin/coupon/", "");
        if("create".equals(op)) {
            create(req,resp);
        }

    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        String requestBody = reader.readLine();
        String name = JacksonUtil.parseString(requestBody,"name");
        String total = JacksonUtil.parseString(requestBody,"total");
        String discount = JacksonUtil.parseString(requestBody,"discount");
        String min = JacksonUtil.parseString(requestBody,"min");
        String limit = JacksonUtil.parseString(requestBody,"limit");
        String type = JacksonUtil.parseString(requestBody,"type");
        String status = JacksonUtil.parseString(requestBody,"status");
        String goodsType = JacksonUtil.parseString(requestBody,"goodsType");
        List<Integer> goodsValue = JacksonUtil.parseIntegerList(requestBody, "goodsValue");
        String timeType = JacksonUtil.parseString(requestBody,"timeType");
        String days = JacksonUtil.parseString(requestBody,"days");
        String startTime = JacksonUtil.parseString(requestBody,"startTime");
        String endTime = JacksonUtil.parseString(requestBody,"endTime");


        MarketCoupon marketCoupon = adminCouponService.create(name,total,discount,min,limit,type,status,goodsType,goodsValue
        ,timeType,days,startTime,endTime);

        Object ok = ResponseUtil.ok(marketCoupon);
        String s = JacksonUtil.writeValueAsString(ok);
        resp.getWriter().println(s);

    }
}
